package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysVote;
import com.ruoyi.system.service.ISysVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * 投票Controller
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@RestController
@RequestMapping("/system/vote")
public class SysVoteController extends BaseController {
    @Autowired
    private ISysVoteService sysVoteService;
    @Autowired
    WxSignatureUtil wxSignatureUtil;

    /**
     * 新增投票十个
     */
    @Log(title = "投票", businessType = BusinessType.INSERT)
    @PostMapping("toupiao")
    public AjaxResult toupiao(@RequestBody SysVote sysVote) {
        Long loginUser = SecurityUtils.getUserId();
        sysVote.setVoteUserId(loginUser);
        return sysVoteService.toupiao(sysVote);
    }

    @PostMapping("/config")
    public AjaxResult config(String url) throws UnsupportedEncodingException {
        String decodedURL = URLDecoder.decode(url, "UTF-8");
        try {
            Map<String, String> returnMap = wxSignatureUtil.getWechatSignature(decodedURL);
            return AjaxResult.success(returnMap) ;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 查询投票列表
     */
    @PreAuthorize("@ss.hasPermi('system:vote:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysVote sysVote) {
        startPage();
        List<SysVote> list = sysVoteService.selectSysVoteList(sysVote);
        return getDataTable(list);
    }

    /**
     * 导出投票列表
     */
    @PreAuthorize("@ss.hasPermi('system:vote:export')")
    @Log(title = "投票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVote sysVote) {
        List<SysVote> list = sysVoteService.selectSysVoteList(sysVote);
        ExcelUtil<SysVote> util = new ExcelUtil<SysVote>(SysVote.class);
        util.exportExcel(response, list, "投票数据");
    }

    /**
     * 获取投票详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:vote:query')")
    @GetMapping(value = "/{voteKey}")
    public AjaxResult getInfo(@PathVariable("voteKey") String voteKey) {
        return success(sysVoteService.selectSysVoteByVoteKey(voteKey));
    }

    /**
     * 新增投票
     */
    @PreAuthorize("@ss.hasPermi('system:vote:add')")
    @Log(title = "投票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVote sysVote) {
        return toAjax(sysVoteService.insertSysVote(sysVote));
    }

    /**
     * 修改投票
     */
    @PreAuthorize("@ss.hasPermi('system:vote:edit')")
    @Log(title = "投票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVote sysVote) {
        return toAjax(sysVoteService.updateSysVote(sysVote));
    }

    /**
     * 删除投票
     */
    @PreAuthorize("@ss.hasPermi('system:vote:remove')")
    @Log(title = "投票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{voteKeys}")
    public AjaxResult remove(@PathVariable String[] voteKeys) {
        return toAjax(sysVoteService.deleteSysVoteByVoteKeys(voteKeys));
    }
}
